// pages/profile/proficiency/index.ts
import tran from '../../../utils/language';
import { userInfoStepFour } from '../../../utils/api.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    _t: tran._t('proficiency'),
    _tBase: tran._t('base'),
    navBarHeight: getApp().globalData.navBarHeight,
    sliderData: [{
      label: '项目管理',
      value: 25,
      skillProficiency: 'BASIC',
      skillId: ''
    }],
    levelIcons: {
      basics: '/images/basics.png',
      middleLevel: '/images/middleLevel.png',
      highLevel: '/images/highLevel.png',
      expert: '/images/expert.png',
    },
    isEdit: getApp().globalData.profileFrom === 'edit',
    loginInfo: {
      skills: []
    },
    nextTapFlag: true,
    registerFlag: ''
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options: any) {
    const { isEdit } = this.data
    const loginInfo = wx.getStorageSync('userInfo')
    this.setData({
      loginInfo,
      registerFlag: options.type
    }, () => {
      if (isEdit) {
        this.handleData()
      }
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    const { isEdit } = this.data
    const professionList = wx.getStorageSync('currentProfessionList')
    let currentList = professionList.filter((item: any) => item.selectFlag == true)
    this.handleData()
    // if (isEdit) {
    //   this.handleData()
    // } else {
      currentList.map((item: any) => {
        item.value = 0
        item.skillProficiency = 'BASIC'
        item.skillId = item.tagTypeId
      })
      this.setData({
        sliderData: currentList
      })
    // }
  },
  previousTap () {
    // wx.navigateBack()
    wx.navigateTo({
      url: "/pages/profile/interests-skills/index?type=register"
    })
  },
  /**
   * 处理编辑的时候回显的数据
   */
  handleData() {
    const { loginInfo } = this.data
    const professionList = wx.getStorageSync('currentProfessionList')
    let currentList = professionList.filter((item: any) => item.selectFlag == true)
    
    const { skills } = loginInfo
    currentList.map((item: any) => {
      const list = skills.filter((item1: any) => item1.tagTypeId == item.tagTypeId)
      if (list.length > 0) {
        const obj: any = list[0]
        item.skillProficiency = obj.skillProficiency
        if (obj.skillProficiency == 'BASIC') {
          item.value = 25
        } else if (obj.skillProficiency == 'INTERMEDIATE') {
          item.value = 50
        } else if (obj.skillProficiency == 'ADVANCED') {
          item.value = 75
        } else {
          item.value = 100
        }
      } else {
        item.value = 0
        item.skillProficiency = 'BASIC'
      }
      item.skillId = item.tagTypeId
    })
    this.setData({
      sliderData: currentList
    })
  },
  onChange(e: baseEvent) {
    const index = e.target.dataset.index
    const value = e.detail.value
    let skillProficiency = 'BASIC'
    if (value < 25) { // 
      skillProficiency = 'BASIC'
    } else if (value > 25 && value <= 50) { // INTERMEDIATE
      skillProficiency = 'INTERMEDIATE'
    } else if (value > 50 && value <= 75) { // ADVANCED
      skillProficiency = 'ADVANCED'
    } else { //EXPERT
      skillProficiency = 'EXPERT'
    }
    const newArr = this.data.sliderData
    newArr[index].value = value
    newArr[index].skillProficiency = skillProficiency
    this.setData({ sliderData: newArr })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },
  nextTap() {
    const { sliderData, registerFlag } = this.data
    const list: any = []
    sliderData.map(item => {
      const obj = {
        skillId: item.skillId,
        skillProficiency: item.skillProficiency
      }
      list.push(obj)
    })
    const params = {
      skillProficiency: list
    }
    this.setData({
      nextTapFlag: false
    })
    userInfoStepFour(params).then(res => {
      // 因为请求里面都判断了状态码，所以走到这里，就是成功了
      wx.setStorageSync('professionList', []);
      // wx.showToast({
      //   title: '操作成功'
      // })
      let loginInfo = wx.getStorageSync('loginInfo')
      if (loginInfo.completedStep <= 4) {
        loginInfo.completedStep = 4
        wx.setStorageSync('loginInfo', loginInfo)
      }
      // getApp().getUserInfo()
      wx.navigateTo({
        url: `/pages/profile/personality-type/index?type=${registerFlag}`
      })
      setTimeout(() => {
        this.setData({
          nextTapFlag: true
        })
      }, 1000)
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})